<template>
<header>
  <nav class="container">
    <div class="branding">
      <router-link class="header" :to="{ name:'Home'}">Jinyu_Blog</router-link>
    </div>
    <div class="nav-link">
      <ul v-show="!mobile">
        <router-link class="link" :to="{name: 'Home'}">主页</router-link>
        <router-link class="link" :to="{name: 'Blogs'}">博客</router-link>
        <router-link class="link" v-if="user" :to="{name: 'CreatePost'}">写博客</router-link>
        <router-link v-if="!user" class="link" :to="{name:'Login'}">登录/注册</router-link>
      </ul>
      <div @click="toggleProfileMenu"  :class="{ 'mobile-user-menu': mobile }" v-if="user" class="profile" ref="profile">
        <span>{{user.username.split('')[0]}}</span>
        <div v-show="profileMenu" class="profile-menu">
          <div class="info">
            <p class="initials">{{user.username.split('')[0]}}</p>
            <div class="right">
              <p>{{user.email}}</p>
              <p>{{user.username}}</p>
            </div>
          </div>
          <div class="options">
            <div class="option" @click="closeMenu">
              <router-link class="option" :to="{name:'Profile'}">
                <userIcon class="icon" />
                <p>个人信息</p>
              </router-link>
            </div>
            <div @click="signOut" class="option">
                <signOutIcon class="icon"/>
                <p>退出登录</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </nav>
  <menuIcon @click="toggleMobileNav" class="menu-icon" v-show="mobile"/>
  <transition name=mobile-nav>
    <ul class="mobile-nav" v-show="mobileNav">
      <router-link class="link" :to="{name: 'Home'}">主页</router-link>
      <router-link class="link" :to="{name: 'Blogs'}">博客</router-link>
      <router-link class="link" v-if="user" :to="{name: 'CreatePost'}">写博客</router-link>
      <router-link class="link" v-if="!user" :to="{name:'Login'}">登录/注册</router-link>
    </ul>
  </transition>
</header>
</template>

<script>
import menuIcon from '../assets/Icons/bars-regular.svg'
import userIcon from "../assets/Icons/user-alt-light.svg"
import { mapState } from 'vuex'
import signOutIcon from "../assets/Icons/sign-out-alt-regular.svg"
import { setItem } from '@/utils/storage'
export default {
  name: 'navigation',
  components: {
    menuIcon,
    userIcon,
    signOutIcon
  },
  data(){
    return {
      profileMenu: null,
      mobile: null,
      mobileNav: false,
      windowWidth: false
    }
  },
  created(){
    //检测浏览器的改变
    window.addEventListener('resize',this.checkScreen)
    this.checkScreen()
  },
  methods:{
    checkScreen() {
      this.windowWidth = window.innerWidth;
      // 如果小于750 则变为手机模式
      if (this.windowWidth <= 750){
        this.mobile = true
        return;
      }
      this.mobile = false
      this.mobileNav = false
      return
    },
    // 关闭菜单
    closeMenu(){
      this.profileMenu = !this.profileMenu;
    },
    toggleMobileNav() {
      this.mobileNav = !this.mobileNav
    },
    toggleProfileMenu(e) {
      //只有当点击事件和元素绑定的事件一样的时候才会点击成功
      // e.target = 点击的整个dom 
      if(e.target === this.$refs.profile){
         this.profileMenu = !this.profileMenu;
      }
    },
    signOut(){
      setItem('Blog_USER','')
      this.$store.commit('setUser','')
      this.$store.dispatch('getPost')
      // 当在其他页面退出的时候，退回到页面
      if(this.$route.name !== 'Home'){
        this.$router.push( {name:'Home'} )
      }
    }
  },
  computed:{
    ...mapState(['user'])
  }
}
</script>

<style lang="scss" scoped>
header{
  background-color: #fff;
  padding:0 25px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  z-index: 99;
  .link{
    font-weight: 600;
    padding: 0 8px;
    transition: 0.3s color ease;
    &:hover{
      color: #1eb8b8;
    }
  }

  nav{
    display: flex;
    padding: 25px 0;
    .branding{
      display: flex;
      align-items: center;

      .header{
        font-weight: 600;
        font-size: 24px;
        color: #000;
        text-decoration: none;
      }
    }
    .nav-link{
      position: relative;
      display: flex;
      flex: 1;
      align-items: center;
      justify-content: flex-end;
      ul {
        margin-right: 32px;
        .link{
          margin-right: 32px;
        }
        .link:last-child{
          margin-right: 0;
        }
      }

      .profile{
        position: relative;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        border-radius: 50%;
        color: #fff;
        background-color: #303030;
        
        span {
          pointer-events: none;
        }
        .profile-menu{
          position: absolute;
          top: 65px;
          right: 0;
          width: 240px;
          background-color: #303030;
          box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
          .info{
            display: flex;
            align-items: center;
            padding: 15px;
            border-bottom: 1px solid #fff;
            .initials {
              position: initial; 
              width: 40px;
              height: 40px;
              background-color: #fff;
              color: #303030;
              display: flex;
              align-items: center;
              justify-content: center;
              border-radius: 50%;
            }
            .right {
              flex: 1;
              margin-left: 24px;
              p:nth-child(1) {
                font-size: 14px;
              }
              p:nth-child(2) {
                font-size: 12px;
              }
            }
          }
          .options {
            padding: 15px;
            .option {
              text-decoration: none;
              color: #fff;
              display: flex;
              align-items: center;
              margin-bottom: 12px;
              .icon {
                width: 18px;
                height: auto;
              }
              p {
                font-size: 14px;
                margin-left: 12px;
              }
              &:last-child {
                margin-bottom: 0px;
              }
            }
          }
        }
      }
    }
    .mobile-user-menu {
      margin-right: 40px;
    }
  }

  .menu-icon{
    cursor: pointer;
    position: absolute;
    top: 32px;
    right: 25px;
    height: 25px;
    width: auto;
  } 

  .mobile-nav {
    position: fixed;
    padding: 20px;
    width: 70%;
    max-width: 250px;
    display: flex;
    flex-direction: column;
    height: 100%;
    background-color: #303030;
    top: 0;
    left: 0;
    .link{
      padding: 15px 0;
      color: #fff;
    }
  }
  .mobile-nav-enter-active,
  .mobile-nav-leave-active{
    transition: all 1s ease;
  }
  .mobile-nav-enter {
    transform: translateX(-250px);
  }
  .mobile-nav-enter-to{
    transform:translateX(0px);
  }
  .mobile-nav-leave-to{
    transform:translateX(-250px);
  }
}
</style>
